package com.cnfangmao.masterdata.controller;

import java.util.Map;

import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cnfangmao.masterdata.common.constant.Constant;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.UserUtils;
import com.cnfangmao.masterdata.dto.MasterDataListDto;
import com.cnfangmao.masterdata.dto.PageDto;
import com.cnfangmao.masterdata.service.HistoryMasterDataManagerService;
import com.cnfangmao.masterdata.service.NormalMasterDataManagerService;
import com.cnfangmao.masterdata.service.StageMasterDataManagerService;
import com.cnfangmao.masterdata.vo.MasterDataListPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**    
 * @description: 主数据管理器   
 * @author:	chenxc   
 * @date:   2020年4月22日 下午2:10:43   
 */
@RestController
@Api(tags = "1-6、主数据-主数据管理")
@RequestMapping("/masterdata-manager")
public class MasterDataManagerController {
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	@Autowired
	@Qualifier(Constant.STAGE_MASTERDATA_STORAGE_ENGINE)
	private StageMasterDataManagerService stageMasterDataManagerService;
	
	@Autowired
	@Qualifier(Constant.NORMAL_MASTERDATA_STORAGE_ENGINE)
	private NormalMasterDataManagerService normalMasterDataManagerService;
	
	@Autowired
	@Qualifier(Constant.HISTORY_MASTERDATA_STORAGE_ENGINE)
	private HistoryMasterDataManagerService historyMasterDataManagerService;
	
	@ApiOperation(value = "获取临时存储区主数据列表", notes = "获取临时存储区主数据列表", produces = "application/json")
	@ApiImplicitParam(name = "request", value = "请求报文", required = true, paramType = "body", dataType = "MasterDataListDto")
	@PostMapping("/list-stage.do")
	public ResBody<MasterDataListPageVo> listStage(@Validated @RequestBody @NotNull MasterDataListDto request){
		try{
			PageDto page = new PageDto(request.getPageIndex(), request.getPageSize());
			Map<String, Object> filter = request.getFilter();
			
			MasterDataListPageVo result = this.stageMasterDataManagerService.findPage(request.getMetadataId(),UserUtils.getUserId() , page, filter, null, null);
			
			return new ResBody<MasterDataListPageVo>(ResCodeEnum.SUCCESS.getCode(), "", result);
		}catch(Exception ex){
			logger.error(String.format("run listStage error:", ex.getMessage()));

			return new ResBody<MasterDataListPageVo>(ResCodeEnum.FAIL.getCode(), ex.getMessage(), null);
		}
	}
}
